<?php
	
	/**
	 * Pink Koala Base Generator File.
	 * 
	 * @author Brian Bal <brian@youbeqb.com>
	 * @version 1.0
	 * @package pink-koala
	 */
	
	/**
	 * BaseGenerator class
	 *
	 * @package pink-koala
	 * @author Brian Bal
	 **/
	
	Abstract Class BaseGenerator
	{
		public function __construct($params)
		{
			$this->generate($params);
		}
		
		abstract function generate($params);
		
		public function javascriptDir()
		{
			return PROJECT_DIR . "public" . DS . "javascript" . DS;
		}
		
		public function controllerDir()
		{
			return PROJECT_DIR . "app" . DS . "controllers" . DS;
		}
		
		public function helperDir()
		{
			return PROJECT_DIR . "app" . DS . "helpers" . DS;
		}
		
		public function modelDir()
		{
			return PROJECT_DIR . "app" . DS . "models" . DS;
		}
		
		public function viewDir()
		{
			return PROJECT_DIR . "app" . DS . "views" . DS;
		}
		
		public function createFolder($folder_path)
		{
			if(! is_dir($folder_path))
			{
				mkdir($folder_path);
			}

			return true;
		}
		
		public function writeFile($filename, $content)
		{
			// open the file for writing
			$good_to_write = false;



			// make sure not to overwrite something important
			if( file_exists($filename) )
			{
				echo "\n\tWarning:\n\tFile: $filename already exists\n\tdo you want to overwrite (y/n): ";
				$answer = strtolower(substr(trim(fgets(STDIN)), 0, 1));

				if($answer == "y")
				{
					$good_to_write = true;
				}

				echo "\n\n";
			}
			else
			{
				$good_to_write = true;
			}



			// write to the file of your supposed to
			if($good_to_write)
			{
				echo "\tAdding File\n\t$filename";
				
				if (!$handle = @fopen($filename, 'wb')) {
					echo " - Failed\n";
					echo "\n\n ========================== \nCannot open file ($filename)\n ========================== \n\n";
					exit;
			    }
				
				// Write $content to our opened file.
				if (@fwrite($handle, $content) === FALSE) {
					echo " - Failed\n";
					echo "\n\n ========================== \nCannot write to file ($filename)\n ========================== \n\n";
					exit;
			    }
				
				echo " - Success\n\n";
				
				// close the file handle
				fclose($handle);
			}
			else
			{
				echo "\tSkipped - $filename\n\n";
			}
		}
		
		public function renderFile($template)
		{
			$content = "";
			if(file_exists($template))
			{
				ob_start();
				include($template);
				$content = ob_get_contents();
				ob_end_clean();
			}else{
				trigger_error("Template file not found ($template)", E_USER_ERROR);
			}
			
			return $content;
		}
	}
